<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Post;
use Session;
use Mail;

class PagesController extends Controller
{
    /**
     * 获取index
     */
    public function getIndex()
    {
        $posts = Post::orderby('created_at', 'desc')->limit(4)->get();
        return view('pages.home')->withPosts($posts);
    }

    /**
     * 获取关于数据
     */
    public function getAbout()
    {
        return view('pages.about');
    }

    /**
     * 获取联系人
     */
    public function getContact()
    {
        return view('pages.contact');
    }

    /**
     * 发送联系人
     */
    public function postContact(Request $request)
    {
        $this->validate($request, [
            'email'=>'email',
            'subject'=>'required',
            'message'=>'min:5'
            ], [
            'email'=>'邮箱地址不合法',
            'required'=>'不能为空',
            'min'=>'不能少于:min个字符'
        ]);

        $data = [
            'email'=>$request->email,
            'subject'=>$request->subject,
            'msgBody'=>$request->message
        ];

        // view, view-data, form-to-subject
        Mail::send('emails.contact', $data, function($message) use ($data) {
            $message->from('nice2m@163.com', '五朱de博客');
            $message->to($data['email']);
            $message->subject($data['subject']);
        });

        Session::flash('success', '消息发送成功!');
        return redirect('/contact');
    }
}
